#ifndef OPENTISSUE_IMAGE_UTIL_ANIMATE_CLOUDS_H
#define OPENTISSUE_IMAGE_UTIL_ANIMATE_CLOUDS_H
////////////////////////////////////////////////////////////////////////////////
//
// OpenTissue, A toolbox for physical based simulation and animation.
// Copyright (C) 2005 Department of Computer Science, University of Copenhagen
//
// This file is part of OpenTissue.
//
// OpenTissue is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// any later version.
//
// OpenTissue is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenTissue; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Please send remarks, questions and bug reports to OpenTissue@diku.dk,
// or write to:
//
//    Att: Kenny Erleben and Jon Sporring
//    Department of Computing Science, University of Copenhagen
//    Universitetsparken 1
//    DK-2100 Copenhagen
//    Denmark
//
////////////////////////////////////////////////////////////////////////////////
#if (_MSC_VER >= 1200)
# pragma once
# pragma warning(default: 56 61 62 191 263 264 265 287 289 296 347 529 686)
#endif

#include <OpenTissue/core/math/noise/noise_turbulence.h>
#include <cmath>

namespace OpenTissue
{

 /**
  * Animte Clouds
  *
  * @param image
  * @param time
  * @param freq
  * @param amplitude
  * @param octaves
  */
  template<typename image_type>
  inline void animate_clouds( image_type & image, float time = 0.0f, float freq=4.0f, float amplitude = 0.25f, unsigned int octaves = 4 )
  {
    typedef typename image_type::value_type  value_type;
    float z = time - std::floor(time);
    unsigned int width    = image.width();
    unsigned int height   = image.height();
    unsigned int channels = image.channels();
	noise::Turbulence<float> f( octaves );

    float x_scale = freq / (width-1.0);
    float y_scale = freq / (height-1.0); 

    for(unsigned int j=0;j<height;++j)
      for(unsigned int i=0;i<width;++i)
      {
        float x  = x_scale*(i + 0.5);
        float y  = y_scale*(j + 0.5);
        float f0 = f(x, y, z      );
        float f1 = f(x, y, z - 1.0);
        float f  =  f0*(1.0-z) + z*f1;
        value_type value = static_cast<value_type>( (1.0-y/freq) + f*amplitude );
        for(unsigned int c=0;c<channels;++c)
          image(i,j,c) = value;
      }
  }

} // namespace OpenTissue

//OPENTISSUE_IMAGE_UTIL_ANIMATE_CLOUDS_H
#endif
